package script.msg.move;

import com.df.gameserver.modules.user.UserManager;
import com.df.gameserver.modules.user.entity.Player;
import com.df.gameserver.net.MsgSend;
import com.df.script.IScript;
import com.msg.Move;
import io.netty.channel.Channel;

import java.util.Arrays;


public class C2S_SyncPositionScript implements IScript {


    private final Integer scriptId = -1330431105;


    @Override
    public void excute(Object... args) {
        handleMsg((Channel) args[0], (Move.C2S_SyncPosition) args[1]);
    }

    @Override
    public Integer getScriptId() {
        return scriptId;
    }

    private void handleMsg(Channel channel, Move.C2S_SyncPosition msg) {
        System.out.println("收到同步消息："+msg);
        String userId = msg.getStr();
        Player player = UserManager.getInstance().getPlayer(userId);
        if(player==null) return;
        player.setX( msg.getX());
        player.setY( msg.getY());

        Move.S2C_SyncPosition.Builder builder = Move.S2C_SyncPosition.newBuilder();
        builder.setX(msg.getX())
                .setY(msg.getY())
                .setStr(userId);
//        MsgSend.send(channel, builder);
        MsgSend.broadcastAll(builder,Arrays.asList(userId));
    }

}
